/**
 * 添加用户
 * @author dlfming
 * date 2018/06/06 19:33
 */

"use strict";
define(['app', "config/pathConfig", "layer", "layui"], function (app, pathConfig, layer, layui) {
    app.registerController('ctrl.perManager.add-permission', function ($scope, $http, $state) {
        $scope.permission = {};
        // var getList = function (type, parentId) {
        //     if (type == "parent") {
        //         $http.get(pathConfig.backstagePath.getPath() + "/permission/" + parentId).then(function (response) {
        //             $scope.parent = response.data.data;
        //             if ($scope.parent !== null) {
        //                 $scope.parentId = $scope.parent[0].permissionId + '';
        //             }
        //             $scope.index = response.data.data.length;
        //             var number = new Array($scope.index + 2);
        //             for (var i = 0; i <= $scope.index + 1; i++) {
        //                 number[i] = {num: i};
        //             }
        //             if ($scope.index !== null) {
        //                 $scope.permission.permissionSort = $scope.index[0].num + '';
        //             }
        //         });
        //     }
        //     if (type == "children") {
        //         $http.get(pathConfig.backstagePath.getPath() + "/permission/" + parentId).then(function (response) {
        //             $scope.children = response.data.data;
        //             if ($scope.children !== null) {
        //                 $scope.childrenId = $scope.children[0].permissionId + '';
        //             }
        //             $scope.index = response.data.data.length;
        //             var number = new Array($scope.index + 2);
        //             for (var i = 0; i <= $scope.index + 1; i++) {
        //                 number[i] = {num: i};
        //             }
        //             $scope.index = number;
        //             if ($scope.index !== null) {
        //                 $scope.permission.permissionSort = $scope.index[0].num + '';
        //             }
        //         });
        //     }
        // };
        //
        // $scope.disabled = false;
        //
        // getList('parent', 0);
        // $scope.$watch("parentId", function (parentId) {
        //         $scope.disabled = false;
        //         getList('children', parentId);
        // });
        layui.use('tree', function () {
            var tree = layui.tree({
                elem: '#parentId', //指定元素，生成的树放到哪个元素上
                check: 'checkbox', //勾选风格
                skin: 'as', //设定皮肤
                drag: true,//点击每一项时是否生成提示信息
                checkboxName: 'aa[]',//复选框的name属性值
                checkboxStyle: "",//设置复选框的样式，必须为字符串，css样式怎么写就怎么写
                click: function (item) { //点击节点回调
                    console.log("item")
                },
                onchange: function () {//当当前input发生变化后所执行的回调
                    console.log(this);
                },
                nodes: [ //节点
                    {
                        name: '常用文件夹', //节点名称
                        //	spread: true, //是否是展开状态，true为展开状态
                        href: "http://www.baidu.com",//设置节点跳转的链接，如果不设置则不会跳转
                        target: "_self",//节点链接打开方式
                        alias: 'changyong',
                        data: {//为元素添加额外数据，即在元素上添加data-xxx="yyy"，可选
                            nodeName: "常用文件夹",
                            alias: "changyong"
                        },
                        checkboxValue: 1,//复选框的值
                        checked: true,//复选框默认是否选中
                        children: [{
                            name: '所有未读',
                            alias: 'weidu',
                            checked: true,
                            checkboxValue: 2
                        }, {
                            name: '置顶邮件',
                        }, {
                            name: '标签邮件',
                            checked: false,
                            checkboxValue: 3
                        }]
                    }, {
                        name: '我的邮箱',
                        checked: true,
                        spread: true,
                        data: {
                            nodeName: "我的邮箱",
                            alias: "email"
                        },
                        children: [{
                            name: 'QQ邮箱',
                            checked: true,
                            checkboxValue: 4,
                            spread: true,
                            children: [{
                                name: '收件箱',
                                checked: false,
                                checkboxValue: 5,
                                children: [{
                                    name: '所有未读',
                                    checked: false,
                                    checkboxValue: 6,
                                    children: [{
                                        name: '一周未读',
                                        checked: false,
                                        checkboxValue: 6
                                    }]
                                }, {
                                    name: '置顶邮件',
                                    checked: false,
                                    checkboxValue: 7
                                }, {
                                    name: '标签邮件',
                                    checked: false,
                                    checkboxValue: 8
                                }]
                            }, {
                                name: '已发出的邮件',
                                checked: false,
                                checkboxValue: 9
                            }, {
                                name: '垃圾邮件',
                                checked: false,
                                checkboxValue: 10
                            }]
                        }, {
                            name: '阿里云邮',
                            checked: true,
                            checkboxValue: 11,
                            children: [{
                                name: '收件箱',
                                checked: true,
                                checkboxValue: 12
                            }, {
                                name: '已发出的邮件',
                                checked: true,
                                checkboxValue: 13
                            }, {
                                name: '垃圾邮件',
                                checked: true,
                                checkboxValue: 14
                            }]
                        }]
                    }
                ]
            });
        });


        $scope.add = function () {
            layer.confirm('确定要保存当前页面的信息吗？', {
                icon: 3,
                title: '保存提示',
                btn: ['确定', '取消']
            }, function () {
                $http.put(pathConfig.backstagePath.getPath() + "/permission/" + $scope.parentId + "/" + $scope.permission.permissionSort).then(function (response) {
                    if (response.status === 200) {
                        layer.msg('添加成功！', {icon: 1});
                    } else if (response.status === 500) {
                        layer.msg("服务器错误！", {icon: 2});
                    }
                    $state.reload("index.add-permission");
                }, function (error) {
                    console.error(error);
                })
            });
        }

    })
})
